package com.edvard.util;

import java.io.File;
import java.sql.Timestamp;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import cn.kuaipan.android.openapi.KuaipanAPI;
import cn.kuaipan.android.sdk.exception.KscException;
import cn.kuaipan.android.sdk.exception.KscRuntimeException;

import com.edvard.jsbackup.R;

/**
 * 上传任务
 * 
 * @author edvard
 *
 */
public class UploadTask {

	private static final String TAG = "UploadTask";
	
	private ProgressDialog dialog;
	private Activity activity;

	public UploadTask(Activity activity) {
		this.activity = activity;
	}

	public void showProgressDialog() {
		dialog = new ProgressDialog(activity);
		dialog.setMessage(activity.getString(R.string.uploading));
		dialog.setCancelable(false);
		dialog.show();
	}

	public void hideProgressDialog() {
		if (dialog != null && dialog.isShowing()) {
			dialog.dismiss();
		}
	}

	protected void displayAlert(String title, String message) {

		AlertDialog.Builder confirm = new AlertDialog.Builder(activity);
		confirm.setTitle(title);
		confirm.setMessage(message);

		confirm.setNegativeButton(activity.getString(R.string.ok),
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {

						dialog.dismiss();
					}
				});

		confirm.show().show();
	}

	protected void displayErrorAlert(String title, String message) {

		AlertDialog.Builder confirm = new AlertDialog.Builder(activity);
		confirm.setTitle(title);
		confirm.setMessage(message);

		confirm.setNegativeButton(activity.getString(R.string.ok),
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {

						activity.finish();
					}
				});

		confirm.show().show();
	}
	
	public void start (RequestBase req) {
		new Upload ().execute(req);
	}
	
	/**
	 * 上传文件类
	 * @author edvard
	 */
	private class Upload extends AsyncTask<RequestBase, Void, ResultBase> {

		protected void onPreExecute() {
			Log.v(TAG, "====Upload task onPreExecute====");
			showProgressDialog();
		}

		protected ResultBase doInBackground(RequestBase... params) {
			Log.v(TAG, "====Upload task doInBackground====");
			if (params == null || params.length != 1) {
				return null;
			}

			final RequestBase req = (RequestBase) params[0];
			final KuaipanAPI api = req.getApi();
			final String filePath = req.getFilePath();
			ResultBase result = new ResultBase();
	
			File file = new java.io.File(filePath);
			if (!file.exists()) {
				return null;
			}
			Date date = new Date();
			Timestamp timeStamp = new Timestamp(date.getTime());
			result.setFilePath(filePath);
			Log.v(TAG , "ready to upload file " + file.getAbsolutePath() + " name:"+file.getName());
			
//			String time = timeStamp.toString().replace(".", "-");
//			time = time.replace(":", "-").trim();			
//			String remoteFilePath = (req.getRemotePath() + File.separator + time + file.getName()).trim();
//			Log.i(TAG , "网盘名字：" + remoteFilePath);
			try {
				//上传数据库文件
				api.upload(file,req.getRemotePath() + File.separator + file.getName(),
						new TransportListener(TransportListener.OPERATION_UPLOAD, "Upload"));
			} catch (KscRuntimeException e) {
				e.printStackTrace();
			} catch (KscException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}				
	
			Log.v(TAG, "====Upload task doInBackground end====");
			return result;
		}

		protected void onPostExecute(ResultBase result) {

			hideProgressDialog ();
			if (result == null) {
				Toast.makeText(activity, "备份出现错误", Toast.LENGTH_LONG).show();
				return;
			}
			if (result.getErrorMsg() != null) {
				displayErrorAlert(
						activity.getString(R.string.upload_failure_title),
						result.getErrorMsg());
			} else {
				displayAlert("备份完成",
						"上传成功");
			}
		}
	}
}
